package com.example.administrator.zhongyichou.view;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.support.v4.content.ContextCompat;
import android.util.AttributeSet;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.example.administrator.zhongyichou.base.BaseActivity;
import com.example.administrator.zhongyichou.R;

import butterknife.BindView;
import butterknife.ButterKnife;

/**
 * Created by Administrator on 2017/2/22.
 */

public class HeaderView extends LinearLayout {
    @BindView(R.id.btn_left)
    TextView btnLeft;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.btn_right)
    TextView btnRight;
    @BindView(R.id.view_right)
    View viewRight;
    @BindView(R.id.view_top)
    View viewTop;
    @BindView(R.id.personal_my_about_rela)
    RelativeLayout personalMyAboutRela;
    private Context mContext;
    public static final int LEFT = R.id.btn_left;
    public static final int TITLE = R.id.title;
    public static final int RIGHT = R.id.view_right;
    public static final int HEAD = R.id.personal_my_about_rela;

    public HeaderView(Context context) {
        super(context);
    }

    public HeaderView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.mContext = context;
        init(context);
    }

    public HeaderView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
    }

    private void init(Context context) {
        View view = inflate(mContext, R.layout.headview, this);
        ButterKnife.bind(this, view);
        LayoutParams lp = (LayoutParams) viewTop.getLayoutParams();
        lp.height = BaseActivity.getStaticStatusBarHeight(context);
        viewTop.setLayoutParams(lp);
    }

    //设置监听
    public void bindOnclickListener(OnClickListener onClickListener) {
        btnLeft.setOnClickListener(onClickListener);
        title.setOnClickListener(onClickListener);
        viewRight.setOnClickListener(onClickListener);
        personalMyAboutRela.setOnClickListener(onClickListener);
    }

    //设置标题
    public void setTitle(String title) {
        this.title.setText(title);
    }

    //设置标题
    public void setTitle(int title) {
        this.title.setBackgroundResource(title);
    }

    /**
     * 设置标题文字颜色
     *
     * @param colorId
     */
    public void setTitleColor(int colorId) {
        title.setTextColor(colorId);
    }

    public void setLeftIconVisibility(int type) {
        btnLeft.setVisibility(type);
    }

    //设置左边图标
    public void setLeftIcon(int drawableId) {
        Drawable backDraw = ContextCompat.getDrawable(mContext, drawableId);
        backDraw.setBounds(0, 0, backDraw.getMinimumWidth(),
                backDraw.getMinimumHeight());
        btnLeft.setCompoundDrawables(backDraw, null, null, null);
    }

    /**
     * 设置左边文字
     *
     * @param content
     */
    public void setLeftText(String content) {
        if (content != null) {
            btnLeft.setText("  " + content);
        }
    }

    /**
     * 设置左边文字颜色
     *
     * @param colorId
     */
    public void setLeftColor(int colorId) {
        btnLeft.setTextColor(mContext.getResources().getColor(colorId));
    }

    //设置右边边图标
    public void setRightIcon(int drawableId) {
        Drawable backDraw = ContextCompat.getDrawable(getContext(), drawableId);
        backDraw.setBounds(0, 0, backDraw.getMinimumWidth(),
                backDraw.getMinimumHeight());
        btnRight.setCompoundDrawables(null, null, backDraw, null);
    }

    //设置右边边图标
    public void setRightLeftIcon(int drawableId) {
        Drawable backDraw = ContextCompat.getDrawable(getContext(), drawableId);
        backDraw.setBounds(0, 0, backDraw.getMinimumWidth(),
                backDraw.getMinimumHeight());
        btnRight.setCompoundDrawables(backDraw, null, null, null);
    }

    //设置右边文字背景
    public void setRightBackground(int drawableId) {
        btnRight.setBackgroundResource(drawableId);
    }

    /**
     * 设置右边文字
     *
     * @param content
     */
    public void setRightText(String content) {
        if (content != null) {
            btnRight.setText(content + " ");
        }
    }

    /**
     * 设置右边文字
     *
     * @param content
     */
    public void setRightText(int content) {
        btnRight.setBackgroundResource(content);
    }

    /**
     * 设置右边文字颜色
     *
     * @param colorId
     */
    public void setRightColor(int colorId) {
        btnRight.setTextColor(mContext.getResources().getColor(colorId));
    }

    /**
     * 设置右边文字颜色
     */
    public void setRightSize(int size) {
        btnRight.setTextSize(size);
    }

    public static int dip2px(Context context, float dpValue) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dpValue * scale + 0.5f);
    }

}
